const Controller = require("./base");
class UserController extends Controller {
  async user() {
    const ctx = this.ctx;
    const userId = ctx.params.id;
    const user = await ctx.service.user.find(userId);
    this.success(user);
  }

  async login() {
    const ctx = this.ctx;
    const { password,username } = ctx.request.body 
     // 生成一个 jwt token 返回回去 如果 results lenght > 0 
    const results = await this.app.mysql.select('user', {
      where:{password,username},
      limit:1,
      offset:0,
    })
    const token = "123456"
    this.success({ results,token});
  }
  async search1() {
    console.log(1);

    const { ctx } = this;
    const { username, id, page, pageSize } = ctx.query;
    var where = this.getWhere({ username, id });
    console.log(where);
    const result = await ctx.service.generic.findAll(
      "user",
      where,
      page,
      pageSize
    );
    this.success(result);
  }

  async search() {
    const { ctx, app } = this;
    const { username, id, page =1, size = 10 } = ctx.query;
    // ORDER BY name ASC, age DESC;
    const results = await this.app.mysql.query(`select * from user ${this.getWhere({ username,id })} limit ${(page - 1) * size},${size}`);
    this.success(results); 
  }
}

module.exports = UserController;
